<?php
/**
 * 商户中心 passport
 */
namespace logic\site;
use \logic\user\IPassport;
class Passport implements IPassport{
    
    
    /**
     * 登录
     */
    public function login($param){         
        $check = [
            'account'   => ['check'=>'must','msg'=>'请输入账号','reg'=>'account'],
            'password'  => ['check'=>'must','msg'=>'请输入密码'],
        ];
        if(strlen(trim($param['password']))!=32)logicE("密码格式错误");
        $data = pVerify($check,$param);        
        $userInfo = D('Admin','site')->find(['account'=> $param['account']]);        
        if(!$userInfo)  logicE("账号/密码错误");
        if($userInfo['status'] != 1) logicE('账户已被管理关闭');
        $desPassword = \logic\site\AdminManage::desPassword($data['password'],$userInfo['passsalt'],true);
        if( $desPassword['password'] != $userInfo['password'] ) {
            $this->logs($userInfo,2);
            logicE("账号/密码错误");
        } 
        $this->logs($userInfo,1);
        $userInfo['id'] = $userInfo['adminid'];
        return $userInfo;
    }
    
    /**
     * 写登录日志
     * @param $status 登录状态 1 成功 2 失败
     */
    private function logs($userInfo,$status){
        $ip = request()->ip();
        $area =  getIPArea($ip);
        $data = [
            'userid'    => $userInfo['adminid'],
            'status'    => $status,
            'ip'        => $ip,
            'area'      => $area['country'].' '.$area['area'],
            'useragent' => request()->server('HTTP_USER_AGENT'),
        ];
        D('LoginLogs','site')->add($data);
    }
    
    
    
}